$(function(){
	$('body').find('*').removeAttr("contentEditable");
	function setLayoutHeight() {
		var $fullcont = $('.fullcont');
		var $top = $fullcont.find('.ued-layout-top');
		var $bottom = $fullcont.find('.ued-layout-bottom');
		var $side = $fullcont.find('.ued-layout-side');
		var $main = $fullcont.find('.ued-layout-main');
		var $topHeight = $top.height() || 0;
		var $bottomHeight = $bottom.height() || 0;
		$side.css({'top': $topHeight});
		$side.css({'bottom': $bottomHeight});
		$main.css({'top': $topHeight});
		$main.css({'bottom': $bottomHeight});
	}
	setLayoutHeight();
	//单选
	var checkRadio = $('.checkbox-radio');
	checkRadio.find('a').on('click',function(){
		$(this).parent().siblings().find('a').removeClass('check');
		$(this).addClass('check')
	});

	//复选
	var checkRadio = $('.checkbox-check');
	checkRadio.find('a').on('click',function(){
		$(this).toggleClass('check')
	});

    //top搜索
	var datas =[{'id':'2','text':'流程平台'},
	            {'id':'4','text':'IT监控平台'},
	            {'id':'3','text':'大数据专家'},
	            {'id':'5','text':'优惠活动'},
	            {'id':'7','text':'能力集成平台'},
	            {'id':'6','text':'API文档'},
	            {'id':'8','text':'abc'},
	            {'id':'9','text':'aabc'},
	            {'id':'10','text':'aaabc'},
	            {'id':'12','text':'aabca'},
	            {'id':'13','text':'acbca'},
	            {'id':'16','text':'IT监控平台'}];
    $('.search-normal').click(function(){
        $(this).hide();
        $(this).siblings('.ued-sch').css({'display': 'inline-block'}).stop().animate({'width': '100%'}, 400,function(){
            $(this).find('.ued-ico-sch').css({'display': 'block'});
            $.selectSuggest('ued-top-search',datas);
        });
    });
    $(document).mousedown(function(e){
        var _con = $('.ued-sch,#ued-top-search-suggest');   // 设置目标区域
        if(!_con.is(e.target) && _con.has(e.target).length === 0){ // Mark 1
            $('.ued-sch').stop().css({'display': 'none'}).animate({'width': '0'}, 400,function(){
                console.log($(this))
                $(this).find('.ued-ico-sch').hide();
                $(this).find('.ued-input').val('');
            });
            $('.search-normal').show();
        }
    });

    var LayoutFull = {
        $uedContainerFull: $('body'),
        init: function() {
            this.initLayoutFull(); // 初始化布局模块高度
            this.initBoxHeight();  // 初始化各模块高度
        },
        initLayoutFull: function() {
            var self = this;

            this.setLayoutFull();
            $(window).resize(function() {
                self.setLayoutFull();
            });
        },
        initBoxHeight: function() {
            var self = this;
            var $uedSideFull = this.$uedContainerFull.find('.ued-side-full');
            var $uedExtFull = this.$uedContainerFull.find('.ued-ext-full');
            var $uedMainFull = this.$uedContainerFull.find('.ued-main-full');

            this.setBoxHeight($uedSideFull);
            this.setBoxHeight($uedExtFull);
            this.setBoxHeight($uedMainFull);
            $(window).resize(function() {
                self.setBoxHeight($uedSideFull);
                self.setBoxHeight($uedExtFull);
                self.setBoxHeight($uedMainFull);
            });
        },
        setBoxHeight: function($el) {
            $el.each(function() {
                var height = $(this).outerHeight();
                var $uedBoxFull = $(this).find('.ued-box-full').not('[data-auto="true"]');
                var $uedBoxFullTop = $(this).find('.ued-box-full-top').not('[data-auto="true"]');
                var itemHeight = $(this).find('.ued-box-full').not('[data-auto="true"]').outerHeight() + 10;
                var itemHeightTop = $(this).find('.ued-box-full-top').not('[data-auto="true"]').outerHeight() + 10;
                var $uedBoxFullAuto = $(this).find('.ued-box-full[data-auto="true"]');

                if ($uedBoxFullAuto.length) {
                    $uedBoxFullAuto.outerHeight((height - $uedBoxFull.length*itemHeight - $uedBoxFullTop.length*itemHeightTop) / $uedBoxFullAuto.length - 10);
                    $uedBoxFullAuto.css({'min-height': 'auto'});
                } else {
                    if ($uedBoxFullTop.length) {
                        $uedBoxFull.outerHeight((height - $uedBoxFullTop.length*itemHeightTop) / $uedBoxFull.length);
                        $uedBoxFull.css({'min-height': 'auto'});
                    } else {
                        $uedBoxFull.outerHeight(height / $uedBoxFull.length);
                        $uedBoxFull.css({'min-height': 'auto'});
                    }
                }
            });
        },
        setLayoutFull: function() {
            var $top = this.$uedContainerFull.find('.ued-top-full');
            var $bottom = this.$uedContainerFull.find('.ued-bottom-full');
            var $uedSideFull = this.$uedContainerFull.find('.ued-side-full');
            var $uedExtFull = this.$uedContainerFull.find('.ued-ext-full');
            var $uedMainFull = this.$uedContainerFull.find('.ued-main-full');
            var $topHeight = ($top.outerHeight() || 0) ? $top.outerHeight() + 10 : 10;
            var $bottomHeight = $bottom.outerHeight() || 0;

            $uedSideFull.css({'top': $topHeight});
            $uedSideFull.css({'bottom': $bottomHeight});
            $uedExtFull.css({'top': $topHeight});
            $uedExtFull.css({'bottom': $bottomHeight});
            $uedMainFull.css({'top': $topHeight});
            $uedMainFull.css({'bottom': $bottomHeight});
        }
    };
    LayoutFull.init();

    $(window).resize(function() {
        LayoutFull.init();
    });

    function setScroll(el) {
        // 判断niceScroll插件是否存在
        if ($(el).niceScroll) {
            $(el).niceScroll({
                styler: 'fb',
                cursorcolor: 'rgba(0,0,0,0.3)',
                cursorwidth: '3',
                cursorborderradius: '0px',
                background: '',
                spacebarenabled: false,
                touchbehavior: true,
                grabcursorenabled: false,
                cursorborder: '0'
            });
        }
        $(el).getNiceScroll().resize();
    };
    setScroll('.JS-scroll');

    function numberRun(id, num, minBits, splitBits) {
        var nums = num || 666666;

        $('.num-run').each(function() {
            var sid = $(this).closest('.row-wrap').attr('data_sid');
            $(this).attr('id', sid);
        });
        var numRun = $('#'+id).numberAnimate({
            num: nums,
            speed: 800,
            symbol: ',',
            minBits: minBits || 7,
            splitBits : splitBits || 3
        });
        setInterval(function(){
            nums += 2;
            numRun.resetData(nums);
        }, 3000);
    }

    $('.num-run').each(function() {
        var id = $(this).attr('id');
        var num = parseInt($(this).attr('data-num'));
        var minBits = parseInt($(this).attr('data-minBits'));
        var splitBits = parseInt($(this).attr('data-splitBits'));
        numberRun(id, num, minBits, splitBits);
    });

    function initProgress(obj, goalAmount, currentAmount) {
        obj.goalProgress({
            goalAmount: goalAmount,
            currentAmount: currentAmount,
            textBefore: '',
            textAfter: ''
        });
    }
    function setProgress($progress) {
        $progress.each(function() {
            if (!$(this).data('goalamount')) return;
            if (!$(this).data('currentamount')) return;
            $(this).html('');
            initProgress($(this), parseInt($(this).data('goalamount')), parseInt($(this).data('currentamount')));
        });
    }
    setProgress($('body').find('.data-progress'));

    var scroll = function(el, auto, time) {
        var $scrollEl = $(el).children('ul').first();
        var childWidth = 410;
        var i = 0;
        var move = function() {
            $scrollEl.stop().animate({
                marginLeft: -childWidth
            }, 1000, function(){
                //function(){}在动画完成时执行的函数，每个元素执行一次。
                $scrollEl.children('li').first().appendTo($scrollEl);
                $scrollEl.css({ marginLeft: 0 });
            });
        };

        if (auto && time) {
            setInterval( move, time );
        }
    };
    scroll('[data-type=timeline]', true, 6000);

    var tableMoreHiddle = function(el) {
        $(el).each(function() {
            var $height = parseInt($(this).find('tr').height());
            var $elHeight = parseInt($(this).parent().height() - 16);
            var len = parseInt($elHeight / $height);
            $(this).css({
                maxHeight: len * $height + 'px'
            });
        });
    };
    setTimeout(function() {
        tableMoreHiddle('.table-hidden-more');
    }, 30);
    $(window).resize(function() {
        tableMoreHiddle('.table-hidden-more');
    });

    $('.side-nav-wrap').sidenav2();
});
